<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.name" placeholder="字典名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="getdepts1">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->

    <el-table  :data="dept" highlight-current-row v-loading="listLoading" @row-dblclick="getDetails" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="index" width="300">
      </el-table-column>
      <el-table-column prop="name" label="数据字典名" width="300" sortable>
      </el-table-column>
      <el-table-column prop="sn" label="编号" width="350" sortable>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pagesize" :total="total" :page="page" style="float:right;">
      </el-pagination>
    </el-col>

    <!--编辑界面-->
    <el-dialog :title="title" :visible.sync= "editFormVisible" :close-on-click-modal="false">
      <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
        <el-form-item label="字典名" prop="name">
          <el-input v-model="editForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item  label="字典编号" prop="name">
          <el-input v-model="editForm.sn" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>
    <!--  子界面  -->
    <el-drawer title="明细" :visible.sync="table" direction="rtl" size="50%">
      <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
        <el-form :inline="true" :model="filters">
          <el-form-item >
            <el-button type="primary" @click="Add">新增</el-button>
          </el-form-item>
        </el-form>
      </el-col>
      <el-table :data="gridData">
        <el-table-column type="index" label="序号" width="200">
        </el-table-column>
        <el-table-column prop="name" label="明细名" width="350" sortable>
        </el-table-column>
        <el-table-column label="操作" width="150">
          <template scope="scope">
            <el-button size="small" @click="updateEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button type="danger" size="small" @click="delete_ming(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-col :span="24" class="toolbar">
        <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange1" :page-size="pagesize1" :total="total1" :page="page1" style="float:right;">
        </el-pagination>
      </el-col>
    </el-drawer>
    <!--  添加隐藏框  -->
    <el-dialog :title="tot" :visible.sync="dialogFormVisible">
      <el-form :model="form" ref="form">
        <el-form-item label="活动名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="put_ming">确 定</el-button>
      </div>
    </el-dialog>
  </section>
</template>
<script>
import util from '../../common/js/util'
//import NProgress from 'nprogress'
import { getUserListPage, removeUser, batchRemoveUser, editUser, addUser } from '../../api/api';

export default {
  data() {
    return {
      tot:"",
      types_id:0,
      dialogFormVisible:false,
      form: {
        name:'',
        types_id:""
      },
      formLabelWidth:'120px',
      /*抽屉*/
      table: false,
      loading: false,
      gridData: [],
      /*抽屉*/
      options: [],
      cities:[],
      filters: {
        name: '',
        types_id:0
      },
      dept: [],
      pagesize:10,
      pagesize1:10,
      total: 0,
      total1: 0,
      page: 1,
      page1:1,
      listLoading: false,
      sels: [],//列表选中列

      editFormVisible: false,//编辑界面是否显示
      title:"",
      editLoading: false,
      editFormRules: {

      },
      //编辑界面数据
      editForm: {
        sn: "",
        name: ''
      }
    }
  },
  methods: {
    get1(){
      this.page1=1
      this.getdet()
    },
    getdet(){
      let para = {
        currentPage: this.page1,
        id:this.types_id,
        pagesize:this.pagesize1
      };
      console.log(para)
      this.$http.post("/det/pagelist",para).then(res=>{
        this.gridData = res.data.data
        this.total1 = res.data.total
        console.log(this.total1)
      })
    },
    //明细点击删除
    delete_ming:function (index, row){
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        //let para = { id: row.id };
        this.$http.delete("/det/department/"+row.id).then(res=>{
          if(res.data.success){
            this.listLoading = false;
            //NProgress.done();
            this.$message({
              message: res.data.msg,
              type: 'success',
            });
            this.get1()
          }else {
            this.listLoading = false;
            //NProgress.done();
            this.$message({
              message: res.data.msg,
              type: 'error'
            });
          }
        })
      }).catch(() => {
      });

    },
    //更改明细
    updateEdit:function(index,row){
      this.tot="编辑明细"
      console.log(row)
      this.dialogFormVisible=true
      this.form = Object.assign({}, row);
    },
    //添加明细
    put_ming: function () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.editLoading = true;
            //NProgress.start();
            this.form.types_id = this.types_id
            let para = Object.assign({}, this.form);
            console.log(para)
            this.$http.post("/det/department",para).then(res=>{
              if(res.data.success){
                this.editLoading = false;
                //NProgress.done();
                this.$message({
                  message: res.data.msg,
                  type: 'success'
                });
                this.getdet()
                this.dialogFormVisible=false
              }else {
                this.editLoading = false;
                //NProgress.done();
                this.$message({
                  message: res.data.msg,
                  type: 'error'
                })
              }
            })
            this.getdet()
            this.$http.get("/det/list/"+this.types_id).then(res=>{
              this.gridData = res.data
              console.log(this.gridData)
            })
            this.$refs['form'].resetFields();
            this.dialogFormVisible=false
          });
        }
      });
    },
    //明细点击增加刚发
    Add(){
      this.tot="添加明细"
      this.dialogFormVisible =true
      this.form={
        name:'',
        types_id:""
      }
    },
    getDetails(row){
      this.table = true
      this.types_id = row.id
      let para = {
        currentPage: this.page1,
        id:this.types_id,
        pagesize:this.pagesize1
      };
      console.log(para)

      this.$http.post("/det/pagelist",para).then(res=>{
        console.log(res)
        this.gridData = res.data.data
        this.total1 = res.data.total
      })
    },
    detail(){
      console.log(1)
    },
    handleCurrentChange(val) {
      console.log(val)
      this.page = val;
      this.getdepts();
      this.page=1
    },
    handleCurrentChange1(val) {
      this.page1 = val;
      console.log(this.page1)
      this.getdet()
      this.page1=1
    },
    //获取用户列表
    getdepts1() {
      this.page=1
      this.getdepts()
    },
    getdepts() {
      let para = {
        currentPage: this.page,
        name: this.filters.name,
        pagesize:this.pagesize
      };
      console.log(para)
      this.$http.post("/sys/pagelist",para).then(res=>{
        console.log(res)
        this.dept = res.data.data
        this.total = res.data.total
      })
    },
    //删除
    handleDel: function (index, row) {
      console.log(row.id)
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        //let para = { id: row.id };
        this.$http.delete("/sys/department/"+row.id).then(res=>{
          console.log(res)
          if(res.data.success){
            this.listLoading = false;
            //NProgress.done();
            this.$message({
              message: res.data.msg,
              type: 'success'
            });
            this.getdepts(1);
          }else {
            this.listLoading = false;
            //NProgress.done();
            this.$message({
              message: res.data.msg,
              type: 'error'
            });
          }
        })
      }).catch(() => {

      });
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      this.TreeQuery()
      this.employeeQuery()
      this.title = "编辑"
      this.editFormVisible = true;
      this.editForm = Object.assign({}, row);

    },
    //显示新增界面
    handleAdd: function () {
      this.TreeQuery()
      this.employeeQuery()
      this.title = "新增"
      this.editFormVisible = true;
      this.editForm = {
        sn: "",
        name: ''
      };
    },
    //编辑
    editSubmit: function () {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.editLoading = true;
            //NProgress.start();
            let para = Object.assign({}, this.editForm);
            console.log(para)
            this.$http.post("/sys/department",para).then(res=>{
              if(res.data.success){
                this.editLoading = false;
                //NProgress.done();
                this.$message({
                  message: res.data.msg,
                  type: 'success'
                });
                this.getdepts(1);
              }else {
                this.editLoading = false;
                //NProgress.done();
                this.$message({
                  message: res.data.msg,
                  type: 'error'
                })
              }
            })
            this.$refs['editForm'].resetFields();
            this.editFormVisible = false;
            this.getdepts();
          });
        }
      });
    },
    //新增
    addSubmit: function () {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.addLoading = true;
            //NProgress.start();
            let para = Object.assign({}, this.addForm);
            para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd');
            addUser(para).then((res) => {
              this.addLoading = false;
              //NProgress.done();
              this.$message({
                message: '提交成功',
                type: 'success'
              });
              this.$refs['addForm'].resetFields();
              this.addFormVisible = false;
              this.getdepts();
            });
          });
        }
      });
    },
    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    //展示用户
    employeeQuery: function (){
      this.$http.get("/emp/employee").then(res=>{
        this.cities = res.data
      })
    },
    //tree展示
    TreeQuery:function(){
      this.$http.get("/dept/tree").then(res=>{

        this.options = res.data
      })
    }
  },
  mounted() {
    this.getdepts();
  }
}

</script>

<style scoped>

</style>